﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Client/NetClient_Socket/NetClient_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Client/NetClient_Socket/NetClient_Error.h"

//g++ -std=gnu++11 -Wall -g Test_TcpClient.cpp -o Test_TcpClient -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client -lNetEngine_BaseLib -lNetClient_Socket -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client,--disable-new-dtags

int TCPTest_Limit()
{
    TCHAR tszSDBuffer[1024];
    memset(tszSDBuffer, '\0', sizeof(tszSDBuffer));

    SOCKET m_Socket;
    if (!NetClient_TCPSelect_Create(_T("127.0.0.1"), 5000, &m_Socket))
    {
        _tprintf_s(_T("连接失败！\n"));
        return -1;
    }
    while (1)
    {
        memset(tszSDBuffer, '\0', sizeof(tszSDBuffer));
        if (NetClient_TCPSelect_SendMsg(m_Socket, "123", 3))
        {
            printf("NetClient_TCPSelect_SendMsg:123\n");
        }
        Sleep(1);
    }

    NetClient_TCPSelect_Close(m_Socket);

    return 1;
}
int TCPTest()
{
    int nMsgLen = 10240;
    TCHAR tszSDBuffer[10000];
    TCHAR tszMsgBuffer[10240];

    memset(tszSDBuffer, '\0', sizeof(tszSDBuffer));

    SOCKET m_Socket;
    if (!NetClient_TCPSelect_Create(_T("192.168.4.36"), 5000, &m_Socket))
    {
        _tprintf_s(_T("连接失败！\n"));
        return -1;
    }
    for (int i = 0;i < 10;i++)
    {
        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
        if (NetClient_TCPSelect_SendMsg(m_Socket, "123456", 6))
        {
            printf("NetClient_TCPSelect_SendMsg:%d\n", nMsgLen);
        }
        if (NetClient_TCPSelect_RecvMsg(m_Socket, tszMsgBuffer,&nMsgLen))
        {
            printf("NetClient_TCPSelect_RecvMsg:%d = %s\n", nMsgLen,tszMsgBuffer);
        }
        Sleep(1000);
    }

    NetClient_TCPSelect_Close(m_Socket);

    return 1;
}
int TCPTestEx()
{
    int nMsgLen = 10240;
    TCHAR tszSDBuffer[10000];
    TCHAR tszMsgBuffer[10240];

    memset(tszSDBuffer, '\0', sizeof(tszSDBuffer));

    XNETHANDLE xhToken;
    if (!NetClient_TCPSelect_StartEx(&xhToken, _T("192.168.4.36"), 5000))
    {
        _tprintf_s(_T("连接失败！\n"));
        return -1;
    }
    for (int i = 0; i < 10; i++)
    {
        int nLen = 6;
        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
        if (NetClient_TCPSelect_SendEx(xhToken, "123456", &nLen))
        {
            printf("NetClient_TCPSelect_SendEx:%d\n", nMsgLen);
        }
        if (NetClient_TCPSelect_RecvEx(xhToken, tszMsgBuffer, &nMsgLen,100))
        {
            printf("NetClient_TCPSelect_RecvEx:%d = %s\n", nMsgLen, tszMsgBuffer);
        }
        Sleep(1000);
    }

    NetClient_TCPSelect_StopEx(xhToken);

    return 1;
}
int Test_UDPClient()
{
    SOCKET hSocket;
    TCHAR tszMsgBuffer[10240];
    NetClient_UDPSelect_Create(&hSocket, "192.168.1.108", 5000);

    for (int i = 0;i < 10240;i++)
    {
        tszMsgBuffer[i] = i;
    }
    while (1)
    {
        if (!NetClient_UDPSelect_SendMsg(hSocket, tszMsgBuffer, 10240))
        {
            printf("errno!\n");
        }
        Sleep(3);
    }
    NetClient_UDPSelect_Close(hSocket);

    return 0;
}

int main()
{
    TCPTest_Limit();
    TCPTestEx();
    TCPTest();
    system("pause");
    return 1;
}
